﻿using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // 加载程序集
            Assembly mathAssembly = Assembly.LoadFrom("Math.dll");

            // 获取类型
            Type mathType = mathAssembly.GetType("Math.MathOperations");

            // 创建实例
            object mathInstance = Activator.CreateInstance(mathType);

            // 获取接口类型
            Type interfaceType = typeof(IMath.IMathOperations);

            // 测试各个方法
            double a = 10.5, b = 2.5;

            Console.WriteLine("使用反射调用数学运算:");
            Console.WriteLine($"a = {a}, b = {b}");

            // 调用Add方法
            MethodInfo addMethod = interfaceType.GetMethod("Add");
            double result = (double)addMethod.Invoke(mathInstance, new object[] { a, b });
            Console.WriteLine($"加法结果: {result}");

            // 调用Subtract方法
            MethodInfo subtractMethod = interfaceType.GetMethod("Subtract");
            result = (double)subtractMethod.Invoke(mathInstance, new object[] { a, b });
            Console.WriteLine($"减法结果: {result}");

            // 调用Multiply方法
            MethodInfo multiplyMethod = interfaceType.GetMethod("Multiply");
            result = (double)multiplyMethod.Invoke(mathInstance, new object[] { a, b });
            Console.WriteLine($"乘法结果: {result}");

            // 调用Divide方法
            MethodInfo divideMethod = interfaceType.GetMethod("Divide");
            result = (double)divideMethod.Invoke(mathInstance, new object[] { a, b });
            Console.WriteLine($"除法结果: {result}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }
}